/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.controller.PPOutBoundController;

import me.zhengjie.annotation.Log;
import me.zhengjie.pojo.Resp;
import me.zhengjie.pojo.sqlServer.PPInfo;
import me.zhengjie.pojo.sqlServer.PPOutboundMission;
import me.zhengjie.pojo.sqlServer.PPOutboundMissionHistory;
import me.zhengjie.repository.PPInfoRepository;
import me.zhengjie.repository.PPOutboundMissionHistoryRepository;
import me.zhengjie.service.PPOutboundMissionHistoryService;
import me.zhengjie.service.dto.PPOutboundMissionHistoryQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import me.zhengjie.utils.PageResult;
import me.zhengjie.service.dto.PPOutboundMissionHistoryDto;

/**
* @website https://eladmin.vip
* @author xinglin
* @date 2024-08-14
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "pp历史出库任务管理")
@RequestMapping("/api/ppOutboundMissionHistory")
public class PPOutboundMissionHistoryController {

    private final PPOutboundMissionHistoryService ppOutboundMissionHistoryService;
    private final PPOutboundMissionHistoryRepository ppOutboundMissionHistoryRepository;
    private final PPInfoRepository ppInfoRepository;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('ppOutboundMissionHistory:list')")
    public void exportPpOutboundMissionHistory(HttpServletResponse response, PPOutboundMissionHistoryQueryCriteria criteria) throws IOException {
        ppOutboundMissionHistoryService.download(ppOutboundMissionHistoryService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询pp历史出库任务")
    @ApiOperation("查询pp历史出库任务")
    @PreAuthorize("@el.check('ppOutboundMissionHistory:list')")
    public ResponseEntity<PageResult<PPOutboundMissionHistoryDto>> queryPpOutboundMissionHistory(PPOutboundMissionHistoryQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(ppOutboundMissionHistoryService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增pp历史出库任务")
    @ApiOperation("新增pp历史出库任务")
    @PreAuthorize("@el.check('ppOutboundMissionHistory:add')")
    public ResponseEntity<Object> createPpOutboundMissionHistory(@Validated @RequestBody PPOutboundMissionHistory resources){
        ppOutboundMissionHistoryService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改pp历史出库任务")
    @ApiOperation("修改pp历史出库任务")
    @PreAuthorize("@el.check('ppOutboundMissionHistory:edit')")
    public ResponseEntity<Object> updatePpOutboundMissionHistory(@Validated @RequestBody PPOutboundMissionHistory resources){
        ppOutboundMissionHistoryService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除pp历史出库任务")
    @ApiOperation("删除pp历史出库任务")
    @PreAuthorize("@el.check('ppOutboundMissionHistory:del')")
    public ResponseEntity<Object> deletePpOutboundMissionHistory(@RequestBody Integer[] ids) {
        ppOutboundMissionHistoryService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    //获取出库任务的物料明细
    @Log("获取出库任务的物料明细")
    @ApiOperation("获取出库库任务的物料明细")
    @GetMapping("/getOutboundDetail/{id}")
    public Resp getInBoundDetail(@PathVariable Integer id) {
        try {
            PPOutboundMissionHistory ppOutboundHistoryMission = ppOutboundMissionHistoryRepository.getById(id);
            PPInfo ppInfo = ppInfoRepository.getById(ppOutboundHistoryMission.getPpId());
            ppInfo.setPpQty(ppOutboundHistoryMission.getPpQty());

            return Resp.builder().data("ppInfo", ppInfo).status(200).msg("查询成功").build();

        }catch (Exception e) {
            return Resp.builder().status(500).msg("查询失败").build();
        }

    }
}